<template>
  <div>
    <div class="wrapper" v-for="(item, index) in tableData" :key="item.id">
      <div class="user">
        <el-avatar class="avatar" :size="50" :src="item.useravatar"></el-avatar>
        <p class="username">{{ item.nikeName }}</p>
        <el-button
          class="movebtn"
          icon="el-icon-delete"
          circle
          @click="handleDelete(item.id)"
        ></el-button>
      </div>
      <input :id="index" class="exp" type="checkbox" />
      <div class="text">
        <label class="btn" :for="index"></label>
        {{ item.content }}
      </div>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      Index: 1,
      isActive: 1,
      total: 0,
      pageNum: 1,
      pageSize: 5,
      tableData: [],
      //获取localStorage中保存的后台返回的用户信息
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
    };
  },
  created() {
    this.load();
  },
  methods: {
    // 点击删除帖子
    move() {},
    load() {
      request
        .get("/mypost/page/"+ this.user.id, {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          Promise.all(
            this.tableData.map((row) => this.getUser(row.userId))
          ).then((accounts) => {
            for (let i = 0; i < this.tableData.length; i++) {
              this.$set(this.tableData[i], "nikeName", accounts[i].nikeName);
              this.$set(this.tableData[i], "useravatar", accounts[i].avatarUrl);
            }
          });
          this.total = res.data.total;
        });
    },
    async getUser(id) {
      if (id != null) {
        try {
          const res = await request.get("/user/" + id);
          if (res.code === 200) {
            return res.data;
          } else {
            this.$notify.error(res.msg);
            return "";
          }
        } catch (error) {
          console.error(error);
          this.$notify.error("获取用户失败");
          return "";
        }
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    handleDelete(id) {
      request.delete("/mypost/"+ id).then((res) => {
        if (res.code === 200) {
          this.$notify.success("删除成功");
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.wrapper {
  position: relative;
  display: flex;
  margin: 25px auto;
  width: 650px;
  overflow: hidden;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 5px 4px 14px #bebebe, -20px -20px 60px #ffffff;
}
.user {
  position: absolute;
  width: 200px;
  text-align: left;
  margin-top: -25px;
  margin-left: 50px;
}
.avatar {
  margin-left: -47px;
  margin-top: 20px;
}
.username {
  color: #333;
  font-size: 20px;
  font-weight: 700;
  font-family: "Microsoft YaHei";
  margin-left: 15px;
  margin-top: -48px;
}
.movebtn {
  float: left;
  margin-left: 547px;
  margin-top: -50px;
}
/* 多行文本超出省略 */
.text {
  font-size: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: justify;
  /* display: flex; */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  position: relative;
  margin-top: 55px;
}
.text::before {
  content: "";
  height: calc(100% - 24px);
  float: right;
}
.text::after {
  content: "";
  width: 999vw;
  height: 999vw;
  position: absolute;
  box-shadow: inset calc(100px - 999vw) calc(30px - 999vw) 0 0 #fff;
  margin-left: -100px;
}
.btn {
  float: right;
  clear: both;
  margin-left: 10px;
  font-size: 16px;
  padding: 0 8px;
  background: #1c92d2;
  line-height: 24px;
  border-radius: 4px;
  color: #fff;
  cursor: pointer;
  /* margin-top: -30px; */
}
.btn::before {
  content: "展开";
}
.exp {
  display: none;
}
.exp:checked + .text {
  -webkit-line-clamp: 999;
}
.exp:checked + .text::after {
  visibility: hidden;
}
.exp:checked + .text .btn::before {
  content: "收起";
}
.nullCss {
  margin: 60px auto;
  font-size: 14px;
  text-align: center;
  color: #999;
}
</style>
